<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="50"
      :immediate-check="false"
>
  <!-- 文章列表 -->
  <article-item   offset="50"
      :immediate-check="false"
      v-for="item in articleList"
      :key="item.art_id"
      :obj="item"
></article-item>
</van-list>
 </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from './components/ArticleItem.vue'
import { articlesListAPI } from '@/api'
import { timeAgo } from '@/utils/date'
export default {
  props: {
    cid: Number
  },
  components: {
    ArticleItem
  },
  methods: {
    async getArticleList () {
      const res = await articlesListAPI({
        channelId: this.cid,
        timestamp: this.nextTime
      })
      res.data.data.results.forEach((obj) => {
        obj.pubdate = this.timeAgo(obj.pubdate)
      })
      if (res.data.data.results.length === 0) {
        this.finished = true
      } else {
        this.nextTime = res.data.data.pre_timestamp
        this.articleList = [...this.articleList, ...res.data.data.results]
        // 等待网络请求结果后
        this.loading = false // 底部加载更多状态
      }
    },
    onRefresh () {
      this.nextTime = null
      this.articleList = []
      this.getArticleList()
      this.isLoading = false
    },
    timeAgo: timeAgo,
    onLoad () {
      if (this.articleList.length > 0) {
        this.getArticleList()
      }
    }
  },
  async created () {
    this.getArticleList()
  },
  data () {
    return {
      articleList: [],
      loading: false, // 底部加载状态
      finished: false, // 数据是否全部加载完成
      timestamp: null,
      nextTime: null,
      isLoading: false // 顶部刷新状态
    }
  }
}
</script>
